package 笔试;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

import javax.swing.ListModel;

import util.LogUtils;
import util.TraverseUtils;

/**
 * 美亚柏科Java笔试
 * @author Administrator
 *
 */
public class MeiYaBaiKe {
	private static final String TAG = "MeiYaBaiKe_Java";

	/**
	 * Question1：扑克牌排序，写出你能想到的时间复杂度最低的方法
	 */
	//生成52张扑克牌
	private List<String> initQuestion1() {
		List<String> poker = new ArrayList<String>();
		for (int i = 2; i <= 14; i ++) {
			if (i <= 10) {
				poker.add("♠" + i);
				poker.add("♣" + i);  
				poker.add("♥" + i);  
				poker.add("♦" + i);  
			}
			if (i == 11) {
				poker.add("♠" + "J");
				poker.add("♣" + "J");  
				poker.add("♥" + "J");  
				poker.add("♦" + "J");  
			}
			if (i == 12) {
				poker.add("♠" + "Q");
				poker.add("♣" + "Q");  
				poker.add("♥" + "Q");  
				poker.add("♦" + "Q");  
			}
			if (i == 13) {
				poker.add("♠" + "K");
				poker.add("♣" + "K");  
				poker.add("♥" + "K");  
				poker.add("♦" + "K");  
			}
			if (i == 14) {
				poker.add("♠" + "A");
				poker.add("♣" + "A");  
				poker.add("♥" + "A");  
				poker.add("♦" + "A");  
			}
		}
		TraverseUtils.println("扑克牌长度>>", poker.size());
		TraverseUtils.printlnOfList(poker);
		return poker;
	}
	private List<Comparator<String>> mListComparators = new ArrayList<Comparator<String>>();
	private List<String> mTypeOrder = new ArrayList<String>();
	private Comparator<String> mComparatorAll = new Comparator<String>() {
		@Override
		public int compare(String a, String b) {
			for (Comparator<String> comp : mListComparators) {
				if (comp.compare(a, b) > 0)	return 1;
				if (comp.compare(a, b) < 0)	return -1;
			}
 			return 0;
		}
	};
	//对扑克牌进行排序
	public void sortPoker(List<String> poker) {

		TreeSet<String> set = new TreeSet<String>(mComparatorAll);
		for (String card : poker) {
			set.add(card);
		}
		TraverseUtils.println("排序結束>>", "嗯。。結束");
		TraverseUtils.println4Set(set);
	}
	private void initListComparators() {
		Comparator<String> compType = new Comparator<String>() {			//對花色進行排序
			@Override
			public int compare(String a, String b) {
				return compareToByType(a, b);
			}
		};
		Comparator<String> compNum = new Comparator<String>() {				//對數字進行排序
			@Override
			public int compare(String a, String b) {
				return compareToByNumber(a, b);
			}
		};
		/*
		 * 注意：add順序不可改變。
		 * 		假如希望先按照花色排序就先add(compType),再add(compNum)
		 * 		假如希望先按照數值排序就先add(compNum) ,再add(compType)
		 */
		mListComparators.add(compType);
		mListComparators.add(compNum);
	}
	//比較花色
	private int compareToByType(String a, String b) {
		String type1 = a.charAt(0)+"", type2 = b.charAt(0)+"";

		int intType1 = StringType2IntegerType(type1), intType2 = StringType2IntegerType(type2);
		return intType1 - intType2;
	}
	//比較數字
	private int compareToByNumber(String a, String b) {
		String num1 = a.substring(1, a.length())+"", num2 = b.substring(1, b.length());
		int intNum1 = StringNumber2IntegerNum(num1), intNum2 = StringNumber2IntegerNum(num2);
		return intNum1 - intNum2;
	}
	private int StringType2IntegerType(String strNum) {
		/*
		 * 注意：花色順序按照 數字排序
		 * 		例如：♣ ♥ ♠ ♦
		 */
		switch (strNum) {
			case "♠":
				return 1;
			case "♣":
				return 2;
			case "♥":
				return 3;
			case "♦":
				return 4;
			default:
				throw new RuntimeException("運行時異常，沒有該花色");
		}
	}
	private int StringNumber2IntegerNum(String strNum) {
		switch (strNum) {
			case "A":
				return 1;
			case "J": 
				return 11;
			case "Q":
				return 12;
			case "K":
				return 13;
			default:
				return Integer.valueOf(strNum);
		}
	}

	public void question1() {
		List<String> poker = initQuestion1();
		initListComparators();
		sortPoker(poker);
	}
;	/**
	 * Question2：输出所有文件目录（文件夹文件遍历）
	 * 	即：樹的遍歷
	 */
	
	/**
	 * Question3：快速排序（好无聊。。）
	 */

	/**
	 * Question4：字符串 空格，字母，特殊字符，數字 
	 * 			過濾出來
	 */
	public void question4(String str) {
		
	}
	public MeiYaBaiKe() {}
	
	
	public static void main(String[] args) {
//		new MeiYaBaiKe().question1();
		new MeiYaBaiKe().question4("zhe shi ge zi fu chuan");
	}

}
